package service.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import service.edu.client.UcenterClient;
import service.edu.entity.Comment;
import service.edu.entity.Member;
import service.edu.entity.vo.CourseInfo;
import service.edu.result.R;
import service.edu.service.CommentService;
import service.edu.utils.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author diao
 * @since 2020-09-23
 */
@RestController
@RequestMapping("/edu/comment")
@CrossOrigin
public class CommentController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private UcenterClient ucenterClient;

    @ApiOperation("评论功能分页查询")
    @GetMapping("/pageQuery/{current}/{limit}")
    public R commentPageQuery(@ApiParam(name = "current", value = "当前页码", required = true)
                              @PathVariable("current")long current,
                              @ApiParam(name = "limit", value = "每页记录数", required = true)
                              @PathVariable("limit")long limit,
                              String courseId){
        Page<Comment> page = new Page<>(current,limit);
       HashMap<String,Object> map= commentService.pageQuery(page,courseId);
        return R.Ok().data(map);
    }

    @ApiOperation("添加评论,需要调用Ucenter模块")
    @PostMapping("/add")
    public R addComment(@RequestBody(required = false) Comment comment, HttpServletRequest request){
        //根据token获取会员id
        String memberId = JwtUtil.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberId)){
            return R.Error().message("请先登录");
        }
        //调用Ucenter模块,根据会员id获取对应的会员信息
        Member member = ucenterClient.getMemberInfoByMemberId(memberId);
        String avatar = member.getAvatar();
        String nickname = member.getNickname();
        //封装数据至评论实体类中
        comment.setAvatar(avatar);
        comment.setNickname(nickname);
        comment.setMemberId(memberId);
        commentService.save(comment);
        return R.Ok();
    }
}

